package com.atguigu.servlet.base;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

public class BaseServlet extends ViewBaseServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        request.setCharacterEncoding("utf-8");
        //flag的值就是方法名
        String flag = request.getParameter("flag");
        //1. 获得当前Servlet类的Class对象(所有的信息都在此对象内封装着[属性、方法、构造器、父类、父接口...])
        Class c=this.getClass();
        //2. 获得一个方法对象(根据方法名和形参列表的类型)
        try {
            Method method = c.getDeclaredMethod(flag, HttpServletRequest.class, HttpServletResponse.class);
            //强制访问
            method.setAccessible(true);
            //3. 如何去调用这个方法
            method.invoke(this,request,response);
        } catch (Exception e) {
            e.printStackTrace();
            //抛出运行时异常 让事务进行处理异常
            throw  new RuntimeException();
        }
    }
}
